package com.example.springboot.service;

import com.example.springboot.document.GoodsDo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

/**
 * @Description: GoodsService
 * @Author wenlinyu
 * @Date: 2020/12/17 9:48
 * @Version 1.0
 */
@Service
@CacheConfig(cacheNames = "GoodsCache")
public class GoodsService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 按id获取商品信息
     * @param id
     * @return
     */
    @Cacheable
    public GoodsDo getById(Long id) {
        logger.info("getById({})", id);
        GoodsDo goods = new GoodsDo();
        goods.setId(id);
        goods.setName("goods-" + id);
        return goods;
    }

    /**
     * 删除商品
     * @param id
     */
    @CacheEvict(key = "#id")
    public void remove(Long id) {
        logger.info("remove({})", id);
    }

    /**
     * 编辑商品信息
     * @param goods
     * @return
     */
    @CachePut(key = "#goods.id")
    public GoodsDo edit(GoodsDo goods) {
        logger.info("edit({})", goods.getId());
        return goods;
    }
}
